package com.briup.corejava.day16;

/**
 * join方法的使用
 */
public class Test3 {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                for (int i = 1; i <=40; i++) {
                    System.out.println(name+"..."+i);
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                for (int i = 100; i >=70; i--) {
                    System.out.println(name+"-"+i);
                    try {
                        if(i == 80){
                            // 阻塞当前线程，让t1先执行
                            t1.join();
                        }
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };
        t1.start();
        t2.start();

        // 主线程每隔7ms输出线程t2的状态
        Thread.State s;
        while((s=t2.getState())!= Thread.State.TERMINATED){
            Thread.sleep(7);
            System.out.println(s);
        }
    }
}
